import { useCounterStore } from './counter';

// 白名单
const whiteList = [
	'/pages/qiangqiang/qiangqiang',
	'/pages/qiangqiang/friendList',
	'/pages/qiangqiang/Headlines',
	'/pages/qiangqiang/hot',
	'/pages/qiangqiang/more',
	'/pages/qiangqiang/popup',
	'/pages/qiangqiang/poster',
	'/pages/qiangqiang/qiangMore',
	'/pages/qiangqiang/recharge',
	'/pages/qiangqiang/reward',
	'/pages/qiangqiang/user'
]

const interception = async () => {
	const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
	// 用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
	list.forEach(item => {
		uni.addInterceptor(item, {
			invoke(e) {
				const counterStore = useCounterStore();
				if(e.url.indexOf('isTimer') != -1){
					//不用执行倒计时
					return;
				}
				const url = e.url.split('?')[0] // 获取要跳转的页面路径（url去掉"?"和"?"后的参数
				if(whiteList.includes(url)){
					counterStore.resetSecondaryAndResumeMain(30);
				}else{
					counterStore.resetCountdowns()
				}
			},
			fail(err) { // 失败回调拦截
				console.log(err)
			}
		})
	})
}
export default interception